/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.fresco.samples.showcase.drawee;

import com.facebook.common.internal.Preconditions;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.ResUtil;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.fresco.samples.showcase.ResourceTable;
import com.facebook.fresco.samples.showcase.common.SimpleScaleTypeDialog;
import com.facebook.fresco.samples.showcase.misc.ImageUriProvider;
import com.oszc.bbhmlibrary.utils.LogUtil;
import com.oszc.bbhmlibrary.utils.MainHandler;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Switch;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.agp.window.dialog.ToastDialog;

import java.util.Arrays;
import java.util.List;

public class DraweeRoundedCornerAbility extends Ability {
    private final String TAG = DraweeRoundedCornerAbility.class.getSimpleName();

    private static final List<ScalingUtils.ScaleType> BITMAP_ONLY_SCALETYPES =
            Arrays.asList(ScalingUtils.ScaleType.CENTER_CROP, ScalingUtils.ScaleType.FOCUS_CROP, ScalingUtils.ScaleType.FIT_XY);

    private ScalingUtils.ScaleType mPreviousScaleType = ScalingUtils.ScaleType.CENTER;
    private int mWindowBackgroundColor;
    private int mColorPrimary;
    private SimpleDraweeView mDraweeRound;
    private SimpleDraweeView mDraweeRadius;
    private SimpleDraweeView mDraweeSome;
    private SimpleDraweeView mDraweeSomeRtl;
    private SimpleDraweeView mDraweeFancy;
    private Switch mShowBordersCheck;
    private Switch mScaleInsideBordersCheck;
    private int mSelIdx = 0;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_layout_drawee_rounded_corner);
        initColors();
        findDrawees();
        MainHandler.postDelay(this::load,500);
    }

    private void load() {
        mDraweeRound.setImageURI(ImageUriProvider.createSampleUri(ImageUriProvider.ImageSize.M, ImageUriProvider.Orientation.LANDSCAPE,null));
        mDraweeRadius.setImageURI(ImageUriProvider.createSampleUri(ImageUriProvider.ImageSize.L,null,null));
        mDraweeSome.setImageURI(ImageUriProvider.createSampleUri(ImageUriProvider.ImageSize.L,null,null));
        mDraweeSomeRtl.setImageURI(ImageUriProvider.createSampleUri(ImageUriProvider.ImageSize.L,null,null));
        mDraweeFancy.setImageURI(ImageUriProvider.createSampleUri(ImageUriProvider.ImageSize.L,null,null));

        Text spinner = (Text) findComponentById(ResourceTable.Id_scaleType);
        spinner.setClickedListener(view -> SimpleScaleTypeDialog.createDialog(this,mSelIdx,((index, entry) -> {
            spinner.setText(entry.description);
            final ScalingUtils.ScaleType scaleType = entry.scaleType;
            mSelIdx = index;
            changeDraweeViewScaleType(mDraweeRound, scaleType,  entry.focusPoint);
            changeDraweeViewScaleType(mDraweeRadius, scaleType, entry.focusPoint);
            changeDraweeViewScaleType(mDraweeSome, scaleType,   entry.focusPoint);
            changeDraweeViewScaleType(mDraweeSomeRtl, scaleType,entry.focusPoint);
            changeDraweeViewScaleType(mDraweeFancy, scaleType,  entry.focusPoint);

            if (BITMAP_ONLY_SCALETYPES.contains(scaleType)
                    && !BITMAP_ONLY_SCALETYPES.contains(mPreviousScaleType)) {
                new ToastDialog(this).setText(ResUtil.getString(this,ResourceTable.String_drawee_rounded_corners_bitmap_only_toast)).show();

            } else if (!BITMAP_ONLY_SCALETYPES.contains(scaleType)
                    && BITMAP_ONLY_SCALETYPES.contains(mPreviousScaleType)) {
                new ToastDialog(this).setText(ResUtil.getString(this,ResourceTable.String_drawee_rounded_corners_overlay_color_toast)).show();
            }
            mPreviousScaleType = scaleType;
        } )));

        mShowBordersCheck = (Switch)findComponentById(ResourceTable.Id_borders);
        mShowBordersCheck.setCheckedStateChangedListener((btnView,checked)->updateRounding());

        mScaleInsideBordersCheck = (Switch)findComponentById(ResourceTable.Id_scaleInside);
        mScaleInsideBordersCheck.setCheckedStateChangedListener((btnView,checked)-> updateRounding());

        final RoundingParams fancyRoundingParams =
                RoundingParams.fromCornersRadii(
                        ResUtil.getDimen(this,ResourceTable.Float_drawee_rounded_corners_fancy_top_left),
                        ResUtil.getDimen(this,ResourceTable.Float_drawee_rounded_corners_fancy_top_right),
                        ResUtil.getDimen(this,ResourceTable.Float_drawee_rounded_corners_fancy_bottom_right),
                        ResUtil.getDimen(this,ResourceTable.Float_drawee_rounded_corners_fancy_bottom_left));
        mDraweeFancy.getHierarchy().setRoundingParams(fancyRoundingParams);
    }

    private void findDrawees() {
        mDraweeRound = (SimpleDraweeView) findComponentById(ResourceTable.Id_drawee_round);
        mDraweeRadius = (SimpleDraweeView) findComponentById(ResourceTable.Id_drawee_radius);
        mDraweeSome = (SimpleDraweeView) findComponentById(ResourceTable.Id_drawee_some);
        mDraweeSomeRtl = (SimpleDraweeView) findComponentById(ResourceTable.Id_drawee_some_rtl);
        mDraweeFancy = (SimpleDraweeView) findComponentById(ResourceTable.Id_drawee_fancy);
    }

    private void initColors() {
        mColorPrimary = new Color(0xffff5722).getValue();
        mWindowBackgroundColor = Color.WHITE.getValue();
    }

    private void changeDraweeViewScaleType(
            SimpleDraweeView draweeView, ScalingUtils.ScaleType scaleType, Point focusPoint) {
        final GenericDraweeHierarchy hierarchy = draweeView.getHierarchy();
        hierarchy.setActualImageScaleType(scaleType);
        hierarchy.setActualImageFocusPoint(focusPoint != null ? focusPoint : new Point(0.5f, 0.5f));

        final RoundingParams roundingParams = Preconditions.checkNotNull(hierarchy.getRoundingParams());
        if (BITMAP_ONLY_SCALETYPES.contains(scaleType)) {
            roundingParams.setRoundingMethod(RoundingParams.RoundingMethod.BITMAP_ONLY);
        } else {
            roundingParams.setOverlayColor(mWindowBackgroundColor);
        }
        hierarchy.setRoundingParams(roundingParams);
    }

    private void updateRounding() {
        boolean showBorder = mShowBordersCheck.isChecked();
        boolean scaleInsideBorder = showBorder && mScaleInsideBordersCheck.isChecked();
        setShowBorder(mDraweeRound, showBorder, scaleInsideBorder);
        setShowBorder(mDraweeRadius, showBorder, scaleInsideBorder);
        setShowBorder(mDraweeSome, showBorder, scaleInsideBorder);
        setShowBorder(mDraweeSomeRtl, showBorder, scaleInsideBorder);
        setShowBorder(mDraweeFancy, showBorder, scaleInsideBorder);
    }

    private void setShowBorder(SimpleDraweeView draweeView, boolean show, boolean scaleInside) {
        LogUtil.error(TAG,"setShowBorder show:"+show + "  scaleInside:"+scaleInside);
        final RoundingParams roundingParams = Preconditions.checkNotNull(draweeView.getHierarchy().getRoundingParams());
        if (show) {
            roundingParams.setBorder(
                    mColorPrimary,
                    ResUtil.getDimen(this,ResourceTable.Float_drawee_rounded_corners_border_width));
            roundingParams.setScaleDownInsideBorders(scaleInside);
        } else {
            roundingParams.setBorder(new Color(0x00ffffff).getValue(), 0);
        }
        draweeView.getHierarchy().setRoundingParams(roundingParams);
    }
}
